package org.changneng.framework.frameworkbusiness.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.changneng.framework.frameworkbusiness.entity.Dept;
import org.changneng.framework.frameworkbusiness.entity.Employee;
import org.changneng.framework.frameworkbusiness.service.DeptService;
import org.changneng.framework.frameworkbusiness.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/")
    public String login(){
        return "login";
    }

    @RequestMapping("/edpSelect")
    public String edpSelect(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5")Integer size,Model model, String empid, String deptno, Integer onduty, String hiredate){

        PageHelper.startPage(page,size);
        List<Dept> depts = deptService.selectAll();
        List<Employee> list = employeeService.selectedp(empid, deptno, onduty,hiredate);
        model.addAttribute("empid",empid);
        model.addAttribute("deptno",deptno);
        model.addAttribute("hiredate",hiredate);
        model.addAttribute("onduty",onduty);
        model.addAttribute("list",list);
        model.addAttribute("deptList",depts);

        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "empList";
    }


    @RequestMapping("/employeeLogin")
    public String loginEmployee(Employee employee, Model model, HttpSession session){
        List<Employee> list = employeeService.Login(employee);

        if (list!=null&&list.size()>0){
            Employee employee1 = list.get(0);
            model.addAttribute("employee1",employee1);
            return "main";
        }
        return "login";
    }


    @RequestMapping("/insertEmployee")
    public String insetEmployee(Employee employee, Model model,String empid, String deptno, Integer onduty, String hiredate){
        List<Dept> depts = deptService.selectAll();
        model.addAttribute("depts",depts);
        int i = employeeService.insert(employee);

        //再查一遍
        List<Employee> list = employeeService.selectedp(empid, deptno, onduty,hiredate);
        model.addAttribute("list",list);
        return "empList";
    }

    //遍历上级
    @RequestMapping("/empdEeptFindAll")
    public String empdEeptFindAll( Model model){
        List<Dept> depts = deptService.selectAll();
        model.addAttribute("depts",depts);
        return "empAdd";
    }





}
